//
//  DouyaPlayerViewController.m
//  BeanSproutEDU
//
//  Created by douya on 15/1/15.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "DouyaPlayerViewController.h"
#import "VideoModel.h"
@interface DouyaPlayerViewController ()

@end

@implementation DouyaPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    count=0;
  _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(loop) userInfo:nil repeats:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoOver) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
   
}

-(void)videoOver
{
    NSLog(@"播放完毕，关闭定时器");
    [_timer invalidate];
}

-(void)loop
{
    double curretTime=floor(self.moviePlayer.currentPlaybackTime);
    double duration=floor(self.moviePlayer.duration);
//    NSLog(@"%f--%f",curretTime,duration);
//    if (curretTime==duration)
//    {
//        if (duration==0)
//        {
//            NSLog(@"还没缓冲好");
//            return;
//        }
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"next" object:_timer];
//    }
    
    if (duration==0)
    {
        return;
    }
    else
    {
        if (curretTime==duration)
        {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"next" object:_timer];
            [self nextVideo];
        }
    }
}
-(void)nextVideo
{
    //连续播放
    NSLog(@"count--%d",count);
    if (count+1==_videoArray.count)
    {
        count=-1;
    }
    count=count+1;
    VideoModel *vModel=_videoArray[count];
    self.moviePlayer.contentURL=[NSURL URLWithString:vModel.video_url];
    [self.moviePlayer play];
    
    /*
     
     这个是弹出UIAlertView 选择重新观看还是下一集，现在暂时不实现这个功能
     _timer=(NSTimer *)notification.object;
     [_timer setFireDate:[NSDate distantFuture]];
     [_player pause];
     [self playerOver:nil];
     
     */
    
}
-(void)playerOver:(MPMoviePlayerController *)player
{
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"是否重新播放？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"下一集", nil];
    
    [alertView show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0://取消
        {
            [self.moviePlayer stop];
        }
            break;
            
        case 1://重新播放;
        {
            [_timer setFireDate:[NSDate distantPast]];
            self.moviePlayer.currentPlaybackTime=0;
            [self.moviePlayer play];
        }
            break;
        case 2://下一集
        {
            [_timer setFireDate:[NSDate distantPast]];
            self.moviePlayer.contentURL=_videoArray[1];
            [self.moviePlayer play];
            
        }
        default:
            break;
    }
}

-(BOOL)shouldAutorotate
{
    return YES;
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
